<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>案例1_电灯开关</title>
</head>
<body>

<img id="light" src="img/off.gif">

<script>

        var light = document.getElementById("light");
        var flag = false; // false 代表灯是灭的，off图片
        light.onclick = function() {
            flag = !flag;
            light.src = flag ? "img/on.gif" : "img/off.gif";
        };

</script>

</body>
</html>